package com.itheima.advantagesup.controller;

import com.itheima.advantagesup.common.PageBean;
import com.itheima.advantagesup.common.R;
import com.itheima.advantagesup.dto.AddArticleDTO;
import com.itheima.advantagesup.dto.ArticlePageDTO;
import com.itheima.advantagesup.entity.YddArticle;
import com.itheima.advantagesup.service.YddArticleService;
import com.itheima.advantagesup.vo.AddArticleVO;
import com.itheima.advantagesup.vo.YddArticleAddVO;
import com.itheima.advantagesup.vo.YddArticleUpdateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Du leilei
 */

@Slf4j
@RequestMapping("/ydd_article")
@RestController
public class YddArticleController {
    @Autowired
    private YddArticleService yddArticleService;

    /**
     * 文章分页查询
     * @param articlePageDTO
     * @return
     */
    @GetMapping("/list")
    public R<AddArticleVO> page(ArticlePageDTO articlePageDTO){
        AddArticleVO addArticleVO = yddArticleService.pageQuery(articlePageDTO);
        return R.success(addArticleVO);
    }

    /**
     * 添加文章
     * @param addArticleDTO
     * @return
     */
   @PostMapping("/add")
    public R<YddArticleAddVO> add(@RequestBody AddArticleDTO addArticleDTO){
       YddArticleAddVO yddArticleAddVO = yddArticleService.add(addArticleDTO);

        return R.success(yddArticleAddVO);
   }

    @GetMapping("/del")
    public R delArticle(Integer id ){
        yddArticleService.delArticle(id);
        return R.success();
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddArticle> queryArticle(Integer id){
        YddArticle yddArticle = yddArticleService.queryArticle(id);
        return R.success(yddArticle);
    }

    /**
     * 修改文章
     */
    @PostMapping("/update")
    public R<YddArticleUpdateVO> updateArticle(@RequestBody YddArticle yddArticle){
        log.info("参数:{}" ,yddArticle);
        YddArticleUpdateVO yddArticleUpdateVO =  yddArticleService.updateArticle(yddArticle);
        return R.success(yddArticleUpdateVO);
    }
}
